package models;

import java.util.*;

import javax.persistence.*;

import org.bouncycastle.asn1.cms.Time;

import play.db.jpa.*;

/**
 * @author Islam Al-aarag
 * @version 1.0
 * 
 */
@Entity
public class Comment extends Model {
	public double rating;
	@Lob
	public String content;
	public Date time;
	@ManyToOne
	public Idea idea;
	@ManyToOne
	public User creator;
	@OneToOne
	public ActionLog generatedLog;
	@OneToMany
	public List<Notification> generatedNotifications;

	/**
	 * 
	 * @param rating
	 *            the rating of this comment (likeness vs hating)
	 * @param content
	 *            the content of the comment
	 * @param time
	 *            the time at which the comment was written
	 * @param creator
	 *            the user who created wrote the comment
	 * @param idea
	 *            the idea under which the comment was written
	 * @param generatedLog
	 *            the action log associated with this comment
	 */

	public Comment(double rating, String content, User creator, Idea idea,
			ActionLog generatedLog) {
		this.rating = rating;
		this.content = content;
		this.time = new Date();
		this.creator = creator;
		this.idea = idea;
		this.generatedLog = generatedLog;
		this.generatedNotifications = new ArrayList<Notification>();
		this.save();
	}

	/**
	 * 
	 * @return
	 * @author Ahmed Mahmoud
	 */
	public boolean cascadeDelete() {
		try {
			idea.comments.remove(this);
			creator.createdComments.remove(this);
			idea.save();
			creator.save();
			this.delete();
		} catch (Exception e) {
			return false;
		}
		return true;
	}
}
